<?php
namespace app\admin\controller;
use think\Controller;
use app\admin\validate;  //使用自定义验证规则  
use Caretree\Caretree;  //无线级分类  在extend文件夹下新建一个Caretree文件在创建一个Caretree.php 命名空间为 包名
use think\db\Query;

class Goods extends Controller 
{
    public function lst()
    {
		$join = [
			['category c','g.category_id = c.id'],
			['brand b','g.brand_id = b.id','LEFT'],
			['type t','g.type_id = t.id','LEFT'],
			['product p','g.id=p.goods_id','LEFT']
		];
		$goodsRes = db('goods')->alias('g')->field('g.*,c.cate_name,b.brand_name,t.type_name,SUM(p.goods_number) gn')->join($join)->group('g.id DESC')->order('id DESC')->paginate(6);
		$this->assign([
			'goodsRes'=>$goodsRes
		]);
		return view('list');
	}
	public function add()
	{
		if(request()->isPost()){
			$data = input('post.');
			$data['time'] = time();// 商品的发布时间
			// dump($data);
			// die;
			//验证
			// $validate = validate('goods');
   //  		if(!$validate->check($data)){
			//     $this->error($validate->getError());
			// }
			$add= model('goods')->save($data);
			if($add){        
				$this->success('添加商品成功!','lst');
			}else{
				$this->error('添加商品失败');
			}
			return;
		}
		//会员级别数据
		$mlRes = db('memberLevel')->field('id,level_name')->select();
		//获取类型
		$typeRes = db('type')->select();
		//获取品牌分类
		$brandRes=db('brand')->field('id,brand_name')->select();
		//商品推荐位
		$goodsRecposRes = db('recpos')->where('rec_type','=',1)->select();
		//商品分类
		$Category = new Caretree();
		//查询当前商品的基本信息
		$CategoryObj = db('Category');
		$CategoryRes = $CategoryObj->order('sort DESC')->select();
		$CategoryRes = $Category->Caretree($CategoryRes);
		$this->assign([
			'mlRes'=>$mlRes,
			'typeRes'=>$typeRes,
			'brandRes'=>$brandRes,
			'CategoryRes'=>$CategoryRes,
			'goodsRecposRes'=>$goodsRecposRes
		]);
		return view();
	}
	public function edit()
	{
			if(request()->isPost()){
				$data = input('post.');
				
				// die;
				//验证
				// $validate = validate('goods');
	//  		if(!$validate->check($data)){
				//     $this->error($validate->getError());
				// }
				$update= model('goods')->update($data);
				if($update){        
					$this->success('修改商品成功!','lst');
				}else{
					$this->error('修改商品失败');
				}
				return;
			}
			//商品id
			$goodsId=input('id');
			//会员级别数据
			$mlRes = db('memberLevel')->field('id,level_name')->select();
			//获取类型
			$typeRes = db('type')->select();
			//获取品牌分类
			$brandRes=db('brand')->field('id,brand_name')->select();
			
			//会员价格
			$_mpRes = db('memberPrice')->where('goods_id','=',$goodsId)->select();
			$mpRes = array();
			foreach($_mpRes as $k => $v){
				$mpRes[$v['mlevel_id']]=$v;
			}
			//商品相册
			$gphotoRes = db('goods_photo')->where('goods_id','=',$goodsId)->select();
			//商品分类
			$Category = new Caretree();
			//查询当前商品基本信息
			$goods = db('goods')->find($goodsId);
			//查询当前商品类型的所有属性信息
			$attrRes = db('attr')->where('type_id','=',$goods['type_id'])->select();
			// dump($attrRes);die;
			//查询当前商品拥有的商品属性goods_attr
			$_gattrRes = db('goods_attr')->where('goods_id','=',$goodsId)->select();
			
			//商品推荐位
			$goodsRecposRes = db('recpos')->where('rec_type','=',1)->select();
			//查询商品的相关推荐位
			$_myGoodsRecposRes = db('rec_item')->where(array('value_type'=>1,'value_id'=>$goodsId))->select();
			
			//改写二维数组为一维数组
			$myGoodsRecposRes = array();
			foreach($_myGoodsRecposRes as $k => $v){
				$myGoodsRecposRes[] = $v['recpos_id'];
			}
			
			//更改二维数组结构为三维数组
			$gattrRes=array();
			foreach ($_gattrRes as $k =>$v){
				$gattrRes[$v['attr_id']][]=$v;
			}
			// dump($gattrRes);die;
			$CategoryObj = db('Category');
			$CategoryRes = $CategoryObj->order('sort DESC')->select();
			$CategoryRes = $Category->Caretree($CategoryRes);
			$this->assign([
				'mlRes'=>$mlRes,
				'typeRes'=>$typeRes,
				'brandRes'=>$brandRes,
				'CategoryRes'=>$CategoryRes,
				'goods' => $goods,
				'mpRes' =>$mpRes,
				'gphotoRes'=>$gphotoRes,
				'attrRes' => $attrRes,
				'gattrRes'=>$gattrRes,
				'goodsRecposRes'=>$goodsRecposRes,
				'myGoodsRecposRes'=>$myGoodsRecposRes
			]);
			return view();
		}
		
		
	public function del($id)
	{
		$del= model('goods')->destroy($id);
		if($del){
			$this->success('删除商品成功','lst');
		}else{
			$this->error('删除商品失败');
		}
	}
	//库存
	public function product($id){
		if(request()->isPost()){
			
			db('product')->where('goods_id','=',$id)->delete();
			$data=input('post.');
			$goodsAttr = $data['goods_attr'];
			$goodsNum = $data['goods_num'];
			$product = db('product');
			// dump($data);
			// die;
			//$k = 0,1,2  $v = 20,20,30
			foreach($goodsNum as $k => $v){
				$strArr = array();
				//$k1 = 颜色，磁盘     $v1 = 数组1 数组2
				foreach($goodsAttr as $k1 => $v1){
					//$v1的数组下标为$k  的取值  
					//如果空选择  跳出两层循环
					if(intval($v1[$k] )<= 0){
						continue  2;
					}
					$strArr[] =$v1[$k];
				}
				
				//数组排序
				sort($strArr);
				// dump($strArr);
				// die;
				$strArr=implode(',',$strArr); //转为字符串
				$product -> insert([
					'goods_id' => $id,
					'goods_number' => $v,
					'goods_attr' => $strArr
				]);
			}
			$this->success('添加库存成功');
			return;
		}
		$_radioAttrRes=db('goods_attr')->alias('g')->field('g.id,g.attr_id,g.attr_value,a.attr_name')->join("attr a",'g.attr_id=a.id')->where(array('g.goods_id'=>$id,'a.attr_type'=>1))->select();
		  
		$radioAttrRes = array();
		//数组格式重组  
		foreach ($_radioAttrRes as $k => $v){
			$radioAttrRes[$v['attr_name']][] = $v;
		}
		 // dump($radioAttrRes);die;
		//获取商品的库存信息
		$goodsProRes = db('product')->where('goods_id','=',$id)->select();
		// dump($goodsProRes);die;
		$this->assign([
			'radioAttrRes'=>$radioAttrRes,
			'goodsProRes' => $goodsProRes
			]);
		return view();
	}
	//异步删除相册图片
	public function ajaxdelpic($id){
		
		$gphoto=db('goods_photo');
		$gphotos=$gphoto->find($id);
		$ogPhoto = IMG_UPLOADS.DS.$gphotos['og_photo'];
		$bigPhoto = IMG_UPLOADS.DS.$gphotos['big_photo'];
		$midPhoto = IMG_UPLOADS.DS.$gphotos['mid_photo'];
		$smPhoto = IMG_UPLOADS.DS.$gphotos['sm_photo'];
		@unlink($ogPhoto);
		@unlink($bigPhoto);
		@unlink($midPhoto);
		@unlink($smPhoto);
		$del = $gphoto->delete($id);
		if($del){
			echo 1;
		}else{
			echo 2;
		}
	}
	
}
